package com.topnav.mc.config;

import com.topnav.mc.interceptor.TraceInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;


/**
 * 说明：因为在子工程中定义的拦截器不注入
 * 所以用WebMvcConfigurationSupport.addInterceptors 来注入
 * @类名: DevApiConfig
 *
 * @author   kenny
 * @Date	 2020年3月4日下午2:03:51
 */
//@Configuration
public class WebMvcConfig extends WebMvcConfigurationSupport {

	@Override
	public void addInterceptors(InterceptorRegistry registry) {

		//注册devapi-core的TraceInterceptor拦截器，在子工程中中拦截器不能注入
		registry.addInterceptor(new TraceInterceptor())
			.addPathPatterns("/**")  //addPathPatterns 用于添加拦截规则
			/** 排除拦截，Security也要写这个例外 */
			.excludePathPatterns("/static/**",
					"/doc.html/**",
					"/webjars/**",
					"/swagger-resources/**",
					"/v2/**",
					"/v3/api-docs/**",
					"/swagger-ui.html/**",
					"/js/**",
					"/error",
					"/api/**",
					"/login") //excludePathPatterns 用户排除拦截
				.excludePathPatterns("/actuator/**","/health","/info");
		super.addInterceptors(registry);
	}

	/**
	 * 全局跨域
	 * @param corsRegistry
	 */
	@Override
	public void addCorsMappings(CorsRegistry corsRegistry){
		/**
		 * 所有请求都允许跨域，使用这种配置就不需要
		 * 在 interceptor 中配置 header 了
		 */
		corsRegistry.addMapping("/**")
				.allowCredentials(true)
				.allowedOrigins("http://localhost:8080")
				.allowedMethods("POST","GET","PUT","OPTIONS","DELETE")
				.allowedHeaders("*")
				.maxAge(3600);
	}

}
